<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\api\model;

use think\Model;

class ShopGoodsModel extends Model
{
    // 确定链接表名
    protected $table = 'xl_shop_goods';

 

   
    /**
     * 商品列表
     * @param $field
     * @param $where
     */
    public function getGoodsList($field,$where)
    {
        return $this->field($field)->where($where)->select();
    }

     /**
     * 获取直播的商品
     * @param $where
     */
    public function getGoodsByWhere($where)
    {
        return $this->field('id,goods_name,goods_brief,cover_img,sale_price,original_price')->where($where)->select();
    }

   
   
    /**
     * 获取单个商品
     * @param $where
     */
    public function getOneGoods($where)
    {
        return $this->where($where)->find();
    }

    //添加商品
    public function addShopGoods($data){
        try{

            $result =  $this->save($data);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, '', '添加成功!');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除商品
     * @param $id
     */
    public function delShopGoods($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除商品成功');

        }catch(PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

     /**
     * 编辑管理员信息
     * @param $param
     */
    public function editShopGoods($param)
    {
        try{

            $result =  $this->save($param, ['id' => $param['id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1,'', '编辑商品成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

}